"""
Case Type   : FDW
Case Name   : 验证create server的wrapper_name 部分测试点合理报错
Create At   : 2024/10/16
Owner       : lonely-dance
Description :
    1.安装mysql_fdw依赖并重启数据库
    2.create extension
    3.create server指定wrapper_name为mysql
    4.create server指定wrapper_name为mysql_fdw
    5.create server指定wrapper_name为fdw
    6.create server指定wrapper_name为其他字符串
    7.清理环境
Expect      :
    1.成功
    2.创建成功
    3.创建成功
    4.创建成功
    5.创建失败
    6.创建失败
    7.成功
History     :
"""

import os
import unittest

from testcase.utils.Common import Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from yat.test import macro
from yat.test import Node

pri = CommonSH('PrimaryDbUser')
system, arch = pri.get_system_arch()
uname = system + '_' + arch
class GrammarFDW(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'---{os.path.basename(__file__)} start---')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.common = Common('PrimaryDbUser')
        self.p_node = Node('PrimaryDbUser')
        self.gauss_home = self.p_node.sh(f'source {macro.DB_ENV_PATH};echo $GAUSSHOME;').result()
        self.p_root_node = Node('PrimaryRoot')
        self.mysql_node = Node('mysql5')
        self.so_file = os.path.join(macro.SCRIPTS_PATH, f"mysql_fdw/{uname}/lib/")
        self.extension_file_path = os.path.join(macro.SCRIPTS_PATH, f"mysql_fdw/{uname}/extension/")
        self.extension_file_names = ['mysql_fdw--1.0--1.1.sql', 'mysql_fdw--1.0.sql', 'mysql_fdw--1.1.sql', 'mysql_fdw.control']
        self.og_lib_path = os.path.join(f'{self.gauss_home}', "lib/postgresql")
        self.og_extension_path = os.path.join(f'{self.gauss_home}', "share/postgresql/extension")
        self.constant = Constant()
        self.og_database = 'fdw'

    def test_grammar_FDW(self):
        text = '-----step1:安装mysql_fdw依赖并重启数据库 expect：成功-----'
        self.log.info(text)
        res = self.common.scp_file(self.p_node, 'mysql_fdw.so', self.og_lib_path, source_dir=self.so_file)
        self.log.info(res)
        self.assertIsNone(res, '执行失败:' + text)
        for file_name in self.extension_file_names:
            res = self.common.scp_file(self.p_node, file_name, self.og_extension_path, source_dir=self.extension_file_path)
            self.log.info(res)
            self.assertIsNone(res, '执行失败:' + text)

        cmd = f"source {macro.DB_ENV_PATH};" \
            f"gs_ssh -c \"gs_guc generate -o usermapping -S default -D $GAUSSHOME/bin\";"
        res = self.p_node.sh(cmd).result()
        self.log.info(res)
        self.assertIn(self.constant.gs_ssh_success_msg, res, '执行失败:' + text)

        restart_res = self.primary_sh.restart_db_cluster()
        self.assertTrue(restart_res, '执行失败:' + text)

        text = '-----step2:create extension expect：创建成功-----'
        self.log.info(text)
        cmd = f"""
                    drop database if exists {self.og_database};
                    create database {self.og_database} with dbcompatibility = 'B' encoding 'utf8';
                """
        res = self.primary_sh.execut_db_sql(cmd)
        self.log.info(res)
        self.assertIn(self.constant.CREATE_DATABASE_SUCCESS, res, '执行失败:' + text)

        cmd = "drop extension if exists mysql_fdw;" \
               "create extension mysql_fdw;"
        self.log.info(cmd)
        res = self.primary_sh.execut_db_sql(cmd, dbname=self.og_database)
        self.log.info(res)
        self.assertIn(self.constant.create_extension_success, res, '执行失败:' + text)
        self.assertIn(self.constant.drop_extension_success, res, '执行失败:' + text)

        text = '-----step3:create server指定wrapper_name为mysql expect：创建成功-----'
        self.log.info(text)
        cmd = "drop server if exists server_fdw_0001_01 cascade;" \
               "create server server_fdw_0001_01 foreign data wrapper mysql;"
        self.log.info(cmd)
        res = self.primary_sh.execut_db_sql(cmd, dbname=self.og_database)
        self.log.info(res)
        self.assertIn(self.constant.drop_server_success, res, '执行失败:' + text)
        self.assertIn(self.constant.create_server_success, res, '执行失败:' + text)

        text = '-----step4:create server指定wrapper_name为mysql_fdw expect：创建成功-----'
        self.log.info(text)
        cmd = "drop server if exists server_fdw_0001_02 cascade;" \
               "create server server_fdw_0001_02 foreign data wrapper mysql_fdw;"
        self.log.info(cmd)
        res = self.primary_sh.execut_db_sql(cmd, dbname=self.og_database)
        self.log.info(res)
        self.assertIn(self.constant.drop_server_success, res, '执行失败:' + text)
        self.assertIn(self.constant.create_server_success, res, '执行失败:' + text)

        text = '-----step5:create server指定wrapper_name为fdw expect：创建失败-----'
        self.log.info(text)
        cmd = "drop server if exists server_fdw_0001_03 cascade;" \
              "create server server_fdw_0001_03 foreign data wrapper fdw;"
        self.log.info(cmd)
        res = self.primary_sh.execut_db_sql(cmd, dbname=self.og_database)
        self.log.info(res)
        self.assertIn(self.constant.drop_server_success, res, '执行失败:' + text)
        self.assertIn("ERROR:  foreign-data wrapper \"fdw\" does not exist", res, '执行失败:' + text)

        text = '-----step6:create server指定wrapper_name为其他字符 expect：创建失败-----'
        self.log.info(text)
        cmd = "drop server if exists server_fdw_0001_04 cascade;" \
              "create server server_fdw_0001_04 foreign data wrapper aabbc;"
        self.log.info(cmd)
        res = self.primary_sh.execut_db_sql(cmd, dbname=self.og_database)
        self.log.info(res)
        self.assertIn(self.constant.drop_server_success, res, '执行失败:' + text)
        self.assertIn("ERROR:  foreign-data wrapper \"aabbc\" does not exist", res, '执行失败:' + text)

    def tearDown(self):
        text = '-----step7:清理环境 expect:成功-----'
        self.log.info(text)
        cmd = '''
            drop server if exists server_fdw_0001_01 cascade;
            drop server if exists server_fdw_0001_02 cascade;
            drop server if exists server_fdw_0001_03 cascade;
            drop server if exists server_fdw_0001_04 cascade;
            drop extension if exists mysql_fdw;
        '''
        self.log.info(cmd)
        res = self.primary_sh.execut_db_sql(cmd, dbname=self.og_database)
        self.log.info(res)
        self.assertTrue(res.count(self.constant.drop_server_success) == 4
                        and self.constant.drop_extension_success in res, '执行失败:' + text)

        cmd = f"rm -rf  {os.path.join(self.og_lib_path, 'mysql_fdw.so')};" \
               f"cd {self.og_extension_path} ;" \
               f"rm -rf  {self.extension_file_names} ;"
        self.log.info(cmd)
        res = self.primary_sh.exec_gs_ssh(cmd)
        self.log.info(res)
        self.assertNotIn('ERROR', res, '执行失败:' + text)

        self.log = Logger()
        self.log.info(f'---{os.path.basename(__file__)} end---')